package com.czl.elm.controller;

import com.czl.elm.domain.UsersLoginParam;
import com.czl.elm.service.UsersLoginService;
import com.czl.elm.util.ActionResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Api(tags = "UsersLoginControllor")
@RestController
@RequestMapping("/login")
public class UsersLoginControllor {

    @Autowired
    private UsersLoginService userLoginService;
    @ApiOperation(value = "密码登录后返回token")
    @PostMapping("passwordlogin")
    public ActionResult login( UsersLoginParam usersLoginParam, HttpServletRequest httpServletRequest){
        return userLoginService.login(usersLoginParam.getPhoneName(),usersLoginParam.getPassword(),usersLoginParam.getCheckCode(),httpServletRequest);
    }

    @ApiOperation(value = "手机登录后返回token")
    @PostMapping("/phonelogin")
    public ActionResult loginbyphone( UsersLoginParam usersLoginParam, HttpServletRequest httpServletRequest){

        return userLoginService.loginbyphone(usersLoginParam.getPhoneName(),usersLoginParam.getCheckCode(),httpServletRequest);
    }

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/getinfo")
    public UsersLoginParam getUserInfo(Principal principal){
        if(null==principal){
            return null;
        }
        String phoneName=principal.getName();
        UsersLoginParam users=userLoginService.getUsersByUserName2(phoneName);
        users.setPassword(null);
        return users;
    }


    @ApiOperation(value="退出登录")
    @PostMapping("/logout")
    public ActionResult logout(){
        return ActionResult.success("注销成功");
    }
}
